clear
set matsize 5000
cd <directory>

**********************************************************************************************************************
**Table 6- Columns 1-2 (Time in and Time Out)
**********************************************************************************************************************

use "inandouttime_sewing_merged_linelevel", clear

drop if year==2010 & month<4
drop if year==2013 & month>6
egen rno=group(unit BATCH)
keep if rno!=.

cap drop date
g date=mdy(month, day, year)
g dow=dow(date)
*Generate factory by year, factory by month, day of the week FE

xi i.unit*i.year i.unit*i.month i.dow
*dropping factories that always had LED and never adopted LED
bys unit: egen mled=mean(led)
drop if mled==0 | mled==1

bys unit: egen mmindateled=min(date) if led==1
bys unit: egen mindateled=mean(mmindateled)
drop mmindateled

g daterelled=date-mindateled

g leddaterelled=led*daterelled
drop if daterelled==.

global depvars timeinmin timeoutmin 


*residualize temperature and rainfall - partial out budgeted efficiency, production line FE, factory by year, factory by month, day of the week FE
foreach i of varlist $depvars{

areg `i'  _I* , cluster(unit) a(rno)
predict `i'res, res

}

foreach i of varlist leddaterelled daterelled led wbgtmeant precipitation{
areg `i'  _I* , cluster(unit) a(rno)
predict `i'res, res

}

*Program for getting confidence intervals from output of boottest
cap program drop getci
program define getci
	matrix ci=r(CI)
	scalar lci=ci[1,1]
	scalar uci=ci[1,2]
	
	end

foreach i of varlist $depvars{

foreach var of varlist led {

cap erase "daterel`var'_rno_timevars_`i'.xml"
cap erase "daterel`var'_rno_timevars_`i'.txt"

cap erase "daterel`var'_rno_timevars_log`i'.xml"
cap erase "daterel`var'_rno_timevars_log`i'.txt"


}
}
	
foreach i of varlist $depvars{

foreach var of varlist led {

	reg `i'res  leddaterel`var'res daterel`var'res `var'res wbgtmeantres precipitationres, cluster(unit) 
			
	su `i' if e(sample)
	local mean=r(mean)
	
	boottest  leddaterel`var'res, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "daterel`var'_rno_timevars_`i'", stats(coef) noaster ctitle("`i'") keep ( leddaterel`var'res) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci),mean of dep var, `mean')

	boottest daterel`var'res, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "daterel`var'_rno_timevars_`i'", stats(coef) noaster ctitle("`i'")  keep (daterel`var'res) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))

	boottest ledres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "daterel`var'_rno_timevars_`i'", stats(coef) noaster ctitle("`i'")  keep (ledres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))
	
	boottest wbgtmeantres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "daterel`var'_rno_timevars_`i'", stats(coef) noaster ctitle("`i'")  keep (wbgtmeantres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))

	boottest precipitationres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "daterel`var'_rno_timevars_`i'", stats(coef) noaster ctitle("`i'")  keep (precipitationres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))
		}
}

**********************************************************************************************************************
**Table 6- Columns 3-6 (Salary Variables)
**********************************************************************************************************************


use "monthlysalary_merged", clear
*only sewing workers
keep if strpos(subdep, "SEWING")
keep if rno!=.
xi i.unit*i.year i.unit*i.month 
*dropping factories that always had LED and never adopted LED

drop if mled==0 | mled==1

*Generate factory by year, factory by month, day of the week FE

xi i.unit*i.year i.unit*i.month i.dow

*monhtly date so need a date relative to led measure that is monthly
g monthdate=ym(year,  month)
drop if monthdate==.

bys unit: egen mindateled=min(monthdate) if led==1
bys unit: egen mindateled1=mean(mindateled)

drop mindateled
ren mindateled1 mindateled

g daterelled=monthdate-mindateled

g leddaterelled=led*daterelled
drop if daterelled==.

g unpaidleave= actc-ectc

foreach i of varlist ectc totded unpaidleave noofpaiddays {

areg `i'  _I* , cluster(unit) a(unittoken)
predict `i'res, res
}

foreach i of varlist leddaterelled daterelled led wbgtmeant precipitation{
areg `i'  _I* , cluster(unit) a(rno)
predict `i'res, res

}

*Program for getting confidence intervals from output of boottest
cap program drop getci
program define getci
	matrix ci=r(CI)
	scalar lci=ci[1,1]
	scalar uci=ci[1,2]
	
	end
	
	
foreach i of varlist ectc totded unpaidleave noofpaiddays {

foreach var of varlist led {

	reg `i'res  leddaterel`var'res daterel`var'res `var'res wbgtmeantres precipitationres, cluster(unit) 
	boottest  leddaterel`var'res, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "daterel`var'_salaryvars_rnodata_`i'", stats(coef) noaster ctitle("`i'") keep ( leddaterel`var'res) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))

	boottest daterel`var'res, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "daterel`var'_salaryvars_rnodata_`i'", stats(coef) noaster ctitle("`i'")  keep (daterel`var'res) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))

	boottest ledres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "daterel`var'_salaryvars_rnodata_`i'", stats(coef) noaster ctitle("`i'")  keep (ledres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))
	
	boottest wbgtmeantres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "daterel`var'_salaryvars_rnodata_`i'", stats(coef) noaster ctitle("`i'")  keep (wbgtmeantres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))

	boottest precipitationres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "daterel`var'_salaryvars_rnodata_`i'", stats(coef) noaster ctitle("`i'")  keep (precipitationres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))
}
}
